package com.dkm.ec.sale.repository.impl;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.dkm.base.api.model.Page;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.gl.persistence.dao.GlCateQueryDao;
import com.dkm.biz.gl.persistence.entity.GlCatePo;
import com.dkm.biz.repository.AbstractRepository;
import com.dkm.ec.sale.domain.SaleReport;
import com.dkm.ec.sale.persistence.dao.SaleReportQueryDao;
import com.dkm.ec.sale.persistence.entity.SaleReportPo;
import com.dkm.ec.sale.repository.SaleReportRepository;

/**
 * 对象功能:skg_sale_report Repository接口的实现类
 * 开发公司:SKG.COM
 * 开发人员:YCM
 * 创建时间:2015-05-07 10:20:33
 */
@Repository
public class SaleReportRepositoryImpl extends AbstractRepository<String, SaleReportPo,SaleReport> implements SaleReportRepository{
	  
	@Resource
	private  SaleReportQueryDao saleReportQueryDao;
	@Resource
	private GlCateQueryDao glCateQueryDao;

	public SaleReport newInstance() {
		SaleReportPo po = new SaleReportPo();
		SaleReport saleReport = SpringHelper.getBean(SaleReport.class);
		saleReport.setData(po);
		return saleReport;
	}

	public SaleReport newInstance(SaleReportPo po) {
		SaleReport saleReport = SpringHelper.getBean(SaleReport.class);
		saleReport.setData(po);
		return saleReport;
	} 
	
	@Override
	protected IQueryDao<String, SaleReportPo> getQueryDao() {
		return saleReportQueryDao;
	}

	@Override
	public List<SaleReportPo> findSaleReportPos(Map<String, Object> params, Page page) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		String cateId = "";
		if(StringUtils.isNotEmpty((String)params.get("thirdCateId"))){
			cateId = (String)params.get("thirdCateId");
		}else if(StringUtils.isNotEmpty((String)params.get("secondCateId"))){
			cateId = (String)params.get("secondCateId");
		}else{
			cateId = (String)params.get("firstCateId");
		}
		if(StringUtils.isNotEmpty(cateId)){
			List<String> cateIds = getCateIds(cateId);
			if(cateIds != null && cateIds.size() > 0){
				paramsMap.put("cateIds", cateIds);
			}
		}
		List<SaleReportPo> saleReportPos = new ArrayList<SaleReportPo>();
		if(page == null){
			saleReportPos = saleReportQueryDao.findAll(paramsMap);
		}else{
			String string=(String)params.get("orderBySql");
			if(StringUtils.isNotEmpty(string)){
				//string.replace("first_cate_", "")
				paramsMap.put("orderBySql", string);
				
			}
			saleReportPos = saleReportQueryDao.findPaged(paramsMap, page);
		}
		
		if(saleReportPos != null && saleReportPos.size() > 0){
			for(SaleReportPo saleReportPo : saleReportPos){
				paramsMap.put("skuKey", saleReportPo.getSkuKey());
				paramsMap.put("startTime", params.get("startTime"));
				paramsMap.put("endTime", params.get("endTime"));
				SaleReportPo saleReportPo2 = saleReportQueryDao.getByKey("findSaleSum", paramsMap);
				if(saleReportPo2 != null){
					saleReportPo.setSaleTotal(saleReportPo2.getSaleTotal());
					saleReportPo.setSaleQty(saleReportPo2.getSaleQty());
					saleReportPo.setSoQty(saleReportPo2.getSoQty());
				}
				
				handleCateName(saleReportPo);
				
				DecimalFormat df = new DecimalFormat("######0.00");
				saleReportPo.setSalePriceStr(df.format(saleReportPo.getSalePrice() == null ? 0 : saleReportPo.getSalePrice()));
				if(saleReportPo.getSaleTotal() != null){
					saleReportPo.setSaleTotalStr(df.format(saleReportPo.getSaleTotal()));
				}else{
					saleReportPo.setSaleTotalStr("0.00");
					saleReportPo.setSaleQty(0);
				}
				
				if("on_sale".equals(saleReportPo.getStatus())){
					saleReportPo.setStatus("在售");
				}else if("not_on_sell".equals(saleReportPo.getStatus())){
					saleReportPo.setStatus("下架");
				}else if("delete".equals(saleReportPo.getStatus())){
					saleReportPo.setStatus("删除");
				}else if("sold_out".equals(saleReportPo.getStatus())){
					saleReportPo.setStatus("售罄");
				}
				
			}
		}
		return saleReportPos;
	}
	
	public void handleCateName(SaleReportPo saleReportPo){
		GlCatePo glCatePo = glCateQueryDao.get(saleReportPo.getCateId());
		if(glCatePo != null){
			int i = glCatePo.getPath().indexOf(saleReportPo.getCateId());
			if(i >= 0){
				String[] cateIdArr = glCatePo.getPath().substring(0, i).split("\\.");
				int index = 0;
				for(int j=0; j<cateIdArr.length; j++){
					GlCatePo po = glCateQueryDao.get(cateIdArr[j]);
					if(po != null){
						saleReportPo.setFirstCate(po.getName());
					}
					index ++;
				}
				if(index == 0){
					saleReportPo.setFirstCate(glCatePo.getName());
				}else if(index == 1){
					saleReportPo.setSecondCate(glCatePo.getName());
				}else if(index == 2){
					saleReportPo.setThirdCate(glCatePo.getName());
				}
			}
		}
	}
	
	public List<String> getCateIds(String cateId){
		Set<String> idSet = new HashSet<String>();
		idSet.add(cateId);
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("cateId", cateId);
		List<GlCatePo> glCatePos = glCateQueryDao.findByKey("findCateIds", paramsMap);
		for(GlCatePo glCatePo : glCatePos){
			String path = glCatePo.getPath();
			int i = path.indexOf(cateId);
			if(i >= 0){
				String[] cateIdArr = path.substring(i).split("\\.");
				for(int j=0; j<cateIdArr.length; j++){
					idSet.add(cateIdArr[j]);
				}
			}
		}
		return new ArrayList<String>(idSet);
	}

	@Override
	public Integer findSaleReportPosCount(Map<String, Object> params) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		String cateId = "";
		if(StringUtils.isNotEmpty((String)params.get("thirdCateId"))){
			cateId = (String)params.get("thirdCateId");
		}else if(StringUtils.isNotEmpty((String)params.get("secondCateId"))){
			cateId = (String)params.get("secondCateId");
		}else{
			cateId = (String)params.get("firstCateId");
		}
		if(StringUtils.isNotEmpty(cateId)){
			List<String> cateIds = getCateIds(cateId);
			if(cateIds != null && cateIds.size() > 0){
				paramsMap.put("cateIds", cateIds);
			}
		}
		Integer count = count(paramsMap);
		return count;
	}

	@Override
	public List<SaleReportPo> getRealTimeLine(Map<String, Object> params) {
		// TODO Auto-generated method stub
		Map<String, Object> paramsMap=new HashMap<String, Object>();
		SimpleDateFormat dfDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Date today = null;
		String dateString=(String)params.get("todayTime");
		if(dateString==null||"".equals(dateString))
		return null;
		try {
			today = dfDateFormat.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(today!=null){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(today);
		int day=calendar.get(calendar.DATE);
		calendar.set(calendar.DATE,day-1);
		Date yestoday=calendar.getTime();
		calendar.setTime(today);
		calendar.set(calendar.DATE,day-7);
		Date lastweekDay=calendar.getTime();
		if(params.get("byDay")!=null&&"1".equals(params.get("byDay")))
		{
			paramsMap.put("yestodayTime", yestoday);
			paramsMap.putAll(params);
			return saleReportQueryDao.findByKey("getRealTimeLineByDay", paramsMap);
		}
		else {
			paramsMap.put("yestodayTime", lastweekDay);
			paramsMap.putAll(params);
			return saleReportQueryDao.findByKey("getRealTimeLineByWeek", paramsMap);
		}
		}
		else{
			return null;
		}
	}

	/**
	 * 获取产品销售排行数据
	 */
	@Override
	public List<SaleReportPo> getSoRankingData(Map<String, Object> params, Page page) {
		return this.saleReportQueryDao.findSoRankingData(params,page);
	}

	@Override
	public Integer countSoRankingData(Map<String, Object> params) {
		
		return this.saleReportQueryDao.countSoRankingData(params);
	}
}
